home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK1HW1.TXT < prev    next >
Text File  |  1996-01-24  |  8KB  |  218 lines

  1. Week 1, homework assignment 1
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6. Refining SALESTAX.BAS
  7. ====================================================================
  8.  
  9. Our SALESTAX.BAS example program provides a good base for us to 
  10. build on.
  11.  
  12. Here it is:
  13.  
  14.  
  15. [start]
  16.     print "Type a dollar and cent amount."
  17.     input "(Press 'Enter' alone for help) "; amount
  18.     if amount = 0 then [help]
  19.     let tax = amount * 0.05
  20.     print "Tax is: "; tax; ". Total is: "; tax + amount
  21.     goto [start]
  22.  
  23. [help]
  24.     cls
  25.     print "SALESTAX.BAS Help"
  26.     print
  27.     print "This tax program determines how much tax is"
  28.     print "due on an amount entered and also computes"
  29.     print "the total amount.  The tax rate is 5%."
  30.     print
  31.     input "Press [Enter] to continue."; dummyVariable
  32.     cls
  33.     goto [start]
  34.  
  35.  
  36. This program calculates a 5% sales tax.  It leaves any rounding
  37. up to the computer user.  We want to extend our SALESTAX program
  38. to round up properly and display our amount paid, tax, and total
  39. amounts in a way suitable for currency.
  40.  
  41.  
  42. Rounding the tax up
  43. --------------------------------------------------------------------
  44.  
  45. Let's define how we will round up tax amounts for our example.
  46. Where I live, I pay 5 cents for every whole dollar.  The remaining
  47. non-whole dollar amount is rounded up or down to the nearest 20
  48. cent amount, and I pay 1 cent for each 20 cents of this rounded
  49. result.  Let's look at it step by step.
  50.  
  51. Given a purchase price of $5.45
  52.  
  53. (a) Strip away the cents from dollars and cents, that gives us $5.
  54.  
  55. (b) Pay 5 cents for each dollar, or 25 cents ($5 * 5 cents).
  56.  
  57. (c) Round the 45 cent portion of our purchase price to the nearest
  58.     multiple of 20, that gives us 40 cents.
  59.  
  60. (d) Now we will pay 1 cent for every 20 cents in that 40 cent
  61.     amount for a total of 2 cents (40 / 20 = 2).
  62.  
  63. (e) Add the 25 cents from (b) to the 2 cents from (d), and we have
  64.     our total tax amount, 27 cents.
  65.  
  66. We can compute the value for step (b) using the INT() function.
  67. Let's see how this works:
  68.  
  69.     'demonstration of INT() function
  70.     input "Enter a non integer value (ie. 3.14) ?"; valueA
  71.     let valueB = int(valueA)
  72.     print "The integer part of "; valueA; " is "; valueB
  73.  
  74. To get the cent amount for the above calculations, we subtract
  75. the integer part from the entered amount, or valueA - valueB.
  76. Let's see how this works:
  77.  
  78.     'getting the non-integer part of a value
  79.     input "Enter a non integer value (ie. 3.14) ?"; valueA
  80.     let valueB = int(valueA)
  81.     print "The non-integer part of "; valueA; " is "; valueA - valueB
  82.  
  83.  
  84. Now that we know how to get the cents out of our dollars and cents
  85. amount, we need to round it to the nearest 20 cent multiple.  Here is
  86. a simple way to round any number to a closest chosen multiple (this
  87. works for values greater than 0):
  88.  
  89. (a) Divide your chosen multiple by two.  We will use this as an
  90.     adjustment value.  If we round 0.14 to the nearest multiple of
  91.     0.20, then our adjustment is 0.10 (0.20 / 2 = 0.10).
  92.  
  93. (b) Add the adjustment value to the number we want to round.  If
  94.     our cent amount is 0.45 then our adjusted amount is 0.55.
  95.  
  96. (c) Now divide the adjusted amount by the multiple we want to round 
  97.     to.  This will give us 2.75 (0.55 / 0.20 = 2.75).
  98.  
  99. (d) Compute the integer portion of 2.75 and we have 2.  Take this
  100.     value and multiply it by 0.20 (our chosen multiple), and we
  101.     get 0.40 (2 * 0.20 = 0.40).
  102.  
  103.  
  104. So we see that 0.45 rounded to the nearest multiple of 0.20 is 0.40.
  105. Here is an expanded version of the program above that shows how to
  106. round a number in the way outline above:
  107.  
  108.     'getting the non-integer part of a value
  109.     input "Enter a non integer value (ie. 3.14) ?"; valueA
  110.     let valueB = int(valueA)
  111.     valueC = valueA - valueB
  112.     print "The non-integer part of "; valueA; " is "; valueC
  113.  
  114.     'now round valueC to the nearest multiple of 0.20
  115.     adjustment = 0.20 / 2
  116.     adjustedC = valueC + adjustment
  117.     multiples = int(adjustedC / 0.20)
  118.     roundedC = multiples * 0.20
  119.     print valueC; " rounded to the closest 0.20 is "; roundedC
  120.  
  121.  
  122. Displaying Monetary Values
  123. --------------------------------------------------------------------
  124.  
  125. When you buy something at the store, the cashier will usually give
  126. you a receipt for your purchase.  The product price, sales tax, and
  127. total amount paid are formatted something like this:
  128.  
  129. Product Price     4.95
  130. Sales Tax         0.25
  131. ----------------------
  132. Total             5.20
  133.  
  134. We want our SALESTAX program to produce information in a similar
  135. fashion.  Notice that the numbers are all aligned so that their
  136. decimal points are one under another.  This is called justification.
  137. Liberty BASIC provides a function for justifying numeric values
  138. called USING().  Here is a quick little program that demonstrates
  139. the USING() function:
  140.  
  141.     'display 3 values justified
  142.     valueA = 0.9
  143.     valueB = 120
  144.     print using("#####.##", valueA)
  145.     print using("#####.##", valueB)
  146.     print using("#####.##", valueA + valueB)
  147.  
  148. Running this program produces:
  149.  
  150.     0.90
  151.   120.00
  152.   120.90
  153.  
  154. Notice the string literal "#####.##".  The USING() function uses
  155. this as a template for formatting a value.
  156.  
  157. The 5 # characters before the period in "#####.##" tell USING() to
  158. place extra spaces in front of the number to ensure that there will
  159. be 5 places before the decimal point.
  160.  
  161. The 2 # characters after the decimal point in "#####.##" tell
  162. USING() to add zeros after the decimal point if there are none, or
  163. to drop all digits after the second digit following the decimal
  164. point.
  165.  
  166.  
  167. Loss of Precision
  168. --------------------------------------------------------------------
  169.  
  170. In the realm of computers, real numbers are usually represented in
  171. what's called single precision floating point format.  This format
  172. is not an absolutely accurate way to represent the value of real
  173. numbers.  Sometimes in the process of executing BASIC code to arrive
  174. at a result, some loss of precision will become apparent.  This
  175. will usually manifest itself as a loss of the minutest amount of a
  176. value.  For example, a result that should be 1.5 becomes 1.499999.
  177.  
  178. For many applications, this is not a very significant problem.  In
  179. the realm of money though, it is a very real problem indeed.  If in
  180. the course of calculating our sales tax the customer owes $0.24 but
  181. the computed result is 0.2399999, it slip through unnoticed.  The
  182. USING() function would remove the 9's after 0.23, and we would never
  183. see it.
  184.  
  185. The solution is to add 0.001 to monetary values whenever the USING()
  186. function is employed.  This is important when using PRINT to display
  187. results, and also saving information to a disk file (this will be
  188. covered later in our course).  Here is how that BASIC code would
  189. look:
  190.  
  191.     'display 3 values justified
  192.     valueA = 0.9
  193.     valueB = 120
  194.     print using("#####.##", valueA + 0.001)
  195.     print using("#####.##", valueB + 0.001)
  196.     print using("#####.##", valueA + valueB + 0.001)
  197.  
  198.  
  199. Here's the assignment
  200. --------------------------------------------------------------------
  201.  
  202. Using the techniques we've just covered, extend the SALESTAX.BAS
  203. program listed at the start of this text and expand it so that...
  204.  
  205.   - It uses the method of tax calculation described above
  206.  
  207.   - It displays a result in sales receipt fashion like so:
  208.  
  209.     Product Price     4.95
  210.     Sales Tax         0.25
  211.     ----------------------
  212.     Total             5.20
  213.  
  214.   - It corrects its calculations for loss of precision
  215.  
  216.  
  217. The solution to this exercise is in the included file WK1HW1.BAS.
  218.